﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;

namespace DAO
{
    public class ChiTietHoaDonDAO
    {
        // ----------------- LOI -----------------------
        public List<ChiTietHoaDonDTO> DanhSachChiTietHoaDonL()
        {
            List<ChiTietHoaDonDTO> listDS = new List<ChiTietHoaDonDTO>();
            string sql = "Select * From CHITIETHOADON";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ChiTietHoaDonDTO hdDTO = new ChiTietHoaDonDTO();
                hdDTO.MaChiTietHoaDon = reader.GetInt32(0);
                hdDTO.MaHoaDon = reader.GetInt32(1);
                hdDTO.MaMatHang = reader.GetInt32(2);
                hdDTO.MaXuatXu = reader.GetInt32(3);
                hdDTO.MaDonViTinh = reader.GetInt32(4);
                hdDTO.SoLuong = reader.GetInt32(5);
                hdDTO.DonGia = reader.GetDouble(6);
                listDS.Add(hdDTO);
            }
            conn.Close();
            return listDS;
        }
        
        public string ThemChiTietHoaDonL(ChiTietHoaDonDTO ctDTO)
        {
            string mess = "";

            string sql = "Insert into ";
            sql += " CHITIETHOADON(MaChiTietHoaDon, MaHoaDon, MaMatHang, MaXuatXu, MaDonViTinh, SoLuong, DonGia)";
            sql += " values(@MaChiTietHoaDon, @MaHoaDon, @MaMatHang, @MaXuatXu, @MaDonViTinh, @SoLuong, @DonGia)";
            SqlConnection conn = DataProvider.ConnectDB();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter para;
                para = cmd.Parameters.Add("@MaChiTietHoaDon", SqlDbType.Int);
                para.Value = ctDTO.MaChiTietHoaDon;
                para = cmd.Parameters.Add("@MaHoaDon", SqlDbType.Int);
                para.Value = ctDTO.MaHoaDon;
                para = cmd.Parameters.Add("@MaMatHang", SqlDbType.Int);
                para.Value = ctDTO.MaMatHang;
                para = cmd.Parameters.Add("@MaXuatXu", SqlDbType.Int);
                para.Value = ctDTO.MaXuatXu;
                para = cmd.Parameters.Add("@MaDonViTinh", SqlDbType.Int);
                para.Value = ctDTO.MaDonViTinh;
                para = cmd.Parameters.Add("@SoLuong", SqlDbType.Int);
                para.Value = ctDTO.SoLuong;
                para = cmd.Parameters.Add("@DonGia", SqlDbType.Float);
                para.Value = ctDTO.DonGia;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                mess = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return mess;
        }
        public int LayMaChiTietHoaDonL()
        {
            int max = 0;
            List<ChiTietHoaDonDTO> listHD = DanhSachChiTietHoaDonL();
            if (listHD.Count < 1)
                return max;
            for (int i = 0; i < listHD.Count; i++)
            {
                if (listHD[i].MaChiTietHoaDon > max)
                    max = listHD[i].MaChiTietHoaDon;
            }
            return max;
        }
        public string XoaChitietHoaDonL(int maCT)
        {
            string mess = "";
            string sql = "Delete * From CHITIETHOADON where MaChiTietHoaDon = " + maCT;
            SqlConnection conn = DataProvider.ConnectDB();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                mess = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return mess;
        }
        public List<MatHangDTO> DanhSachMatHangTheoMaHoaDonL(int maHD)
        {
            string sql = "Select MaMatHang, MaXuatXu, MaDonViTinh, SoLuong, DonGia From CHITIETHOADON Where MaHoaDon = " + maHD;
            List<MatHangDTO> listMH = new List<MatHangDTO>();
            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                MatHangDTO mhDTO = new MatHangDTO();
                mhDTO.MaMatHang = reader.GetInt32(0);
                mhDTO.MaXuatXu = reader.GetInt32(1);
                mhDTO.MaDonViTinh = reader.GetInt32(2);
                mhDTO.SoLuong = reader.GetInt32(3);
                mhDTO.GiaNhap = reader.GetDouble(4);
                listMH.Add(mhDTO);
            }
            conn.Close();
            return listMH;
        }
        // END -------------------- LOI ------------------------
    }
}
